/**
 * ThreadPoolManager.java
 * Created at 2016年11月1日
 * Created by Jackie Liu
 * Copyright (C) 2016 Jackie Liu, All rights reserved.
 */
package com.huabo.thread;

import org.springframework.beans.factory.InitializingBean;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>ClassName: ThreadPoolManager</p>
 * <p>Description: 线程池管理</p>
 * <p>Author: Jackie Liu</p>
 * <p>Date: 2016年11月1日</p>
 */
public class ThreadPoolManager implements InitializingBean {
	
	//线程池中维护的线程最小数量
	private int corePoolSize = 2;
	//线程池中维护的线程最大数量
	private int maxPoolSize = 20;
	//线程池中维护的线程所允许的空闲时间
	private int keepAliveTime = 5;
	//线程池管理类
	private ThreadPoolExecutor threadPool;
	
	public ThreadPoolManager() {}
	
	/**
	 * <p>Description: 执行任务</p>
	 * @param task 子任务
	 */
	public void executeTask(Runnable task) {
		threadPool.execute(task);
	}
	
	public int getCorePoolSize() {
		return corePoolSize;
	}
	public int getMaxPoolSize() {
		return maxPoolSize;
	}
	public int getKeepAliveTime() {
		return keepAliveTime;
	}
	public void setKeepAliveTime(int keepAliveTime) {
		this.keepAliveTime = keepAliveTime;
	}

	/* (non-Javadoc)
	 * <p>Title: afterPropertiesSet</p>
	 * <p>Description: </p>
	 * @throws Exception
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void afterPropertiesSet() throws Exception {
		threadPool = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingDeque());
	}
}
